package bankteam.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.sql.ResultSet;
import java.util.Collection;

import bankteam.excepcion.DAOExcepcion;
import bankteam.interfaces.clienteInterface;
//import bankteam.modelo.Bastanteo;
import bankteam.modelo.Cliente;
//import bankteam.modelo.TipoEmpresa;
import bankteam.util.ConexionBD;
//import bankteam.excepcion.DAOExcepcion;



public class clienteDAO extends BaseDAO  implements clienteInterface {

	@Override
	public void registraCliente(Cliente client)
			throws Exception {

		String query = "INSERT INTO cliente(Cod_Cliente_Interno, RUC,Razon_Social,Cod_Tipo_Empresa" +
				",Fec_Inicio_Ope,Cant_Empleados,Activo) VALUES (?,?,?,?,?,?,?)";
		Connection con = null;
		PreparedStatement stmt = null;
		try {
			
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setDouble(1, client.getCodClienteInterno());
			stmt.setString(2, client.getRuc());
			stmt.setString(3, client.getRazonSocial());
			stmt.setDouble(4, client.getTipoEmpre());
			stmt.setString(5, client.getFecInicioOpe());
			stmt.setDouble(6, client.getCantEmpleados());
			stmt.setDouble(7, 1);
			
			int i = stmt.executeUpdate();
			
			if (i != 1) {
				throw new SQLException("No se pudo insertar");
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			System.out.println(e.getMessage());
		} 
	}
	
	@Override
	public void eliminaCliente(int cod_Cliente_interno) throws DAOExcepcion {
		String query = "update cliente set Activo = '0' WHERE cod_Cliente_interno=?";
		Connection con = null;
		PreparedStatement stmt = null;
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setInt(1, cod_Cliente_interno);
			
			int i = stmt.executeUpdate();

			if (i != 1) {
				throw new SQLException("No se pudo eliminar");
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		}
		finally {
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}	
	}


	@Override
	public void modificaCliente(Cliente client) throws DAOExcepcion {
		String query = "update cliente set RUC=?,Razon_Social=?" +
				",Cod_Tipo_Empresa=?,Fec_Inicio_Ope=?,Cant_Empleados=? where Cod_Cliente_Interno = ?";
		Connection con = null;
		PreparedStatement stmt = null;
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setString(1, client.getRuc());
			stmt.setString(2, client.getRazonSocial());
			stmt.setDouble(3, client.getTipoEmpre());
			stmt.setString(4, client.getFecInicioOpe());
			stmt.setDouble(5, client.getCantEmpleados());
			stmt.setDouble(6, client.getCodClienteInterno());
			
			int i = stmt.executeUpdate();

			if (i != 1) {
				throw new SQLException("No se pudo modificar");
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		}
		finally {
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}	

		
	}
	
	public Collection<Cliente> buscarCodClienteInterno(int codClienteInterno) {
		String query = "SELECT Cod_Cliente, Cod_Cliente_Interno, Ruc, Razon_Social FROM Cliente WHERE Cod_Cliente_Interno = ?";
		Collection<Cliente> lista = new ArrayList<Cliente>();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con  = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setInt(1,codClienteInterno);
			rs = stmt.executeQuery();
			while (rs.next()) {
				Cliente vo = new Cliente();
				vo.setCodCliente(rs.getInt("Cod_Cliente"));
				vo.setCodClienteInterno(rs.getInt("Cod_Cliente_Interno"));
				vo.setRuc(rs.getString("Ruc"));
				vo.setRazonSocial(rs.getString("Razon_Social"));
				lista.add(vo);
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			//throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return lista;
	}

	public Collection<Cliente> buscarRucCliente(String rucCliente) {
		String query = "SELECT Cod_Cliente, Cod_Cliente_Interno, Ruc, Razon_Social FROM Cliente WHERE Ruc = ?";
		Collection<Cliente> lista = new ArrayList<Cliente>();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con  = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setString(1,rucCliente);
			rs = stmt.executeQuery();
			while (rs.next()) {
				Cliente vo = new Cliente();
				vo.setCodCliente(rs.getInt("Cod_Cliente"));
				vo.setCodClienteInterno(rs.getInt("Cod_Cliente_Interno"));
				vo.setRuc(rs.getString("Ruc"));
				vo.setRazonSocial(rs.getString("Razon_Social"));
				lista.add(vo);
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			//throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return lista;
	}

}

